﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Data.Entity;
using Kelper.Domain.Models;
using Kelper.Domain.Data.Abstract;

namespace Kelper.Domain.Data
{
    internal static class Seeder
    {
        public static void Seed(KelperDbContext context)
        {
            Random rnd = new Random((int)DateTime.Now.ToBinary());


            Category food = new Category { Name = "Food" };
            Category drinks = new Category { Name = "Drinks" };
            Category desserts = new Category { Name = "Desserts" };
            context.Categories.Add(food);
            context.Categories.Add(drinks);
            context.Categories.Add(desserts);
            context.SaveChanges();


            List<User> users = new List<User>
            {
                User("Jonatan", "örn", "jonatan.stromsten@gmail.com"),
                User("Johan Ö", "örn", "orjohan@student.chalmers.se"),
                User("Johan E", "örn", "jeklund@student.chalmers.se"),
                User("Alexi", "örn", "alexi@student.chalmers.se")
            };
            users.ForEach(u => context.Users.Add(u));
            context.SaveChanges();

            List<Recipe> recipes = new List<Recipe>
            {
                new Recipe
                {
                    Name = "Very nice drink",
                    Text = "Shake and shake. Drink and enjoy!",
                    Author = users[rnd.Next(users.Count)],
                    Category = drinks,
                    Ingredients = new List<Ingredient>
                    {
                        new Ingredient { Name = "Ice" },
                        new Ingredient { Name = "Vodka" },
                        new Ingredient { Name = "Sugar" }
                    }
                },
                new Recipe
                {
                    Name = "Hangover drink",
                    Text = "Drink. Optionally with some ice.",
                    Author = users[rnd.Next(users.Count)],
                    Category = drinks,
                    Ingredients = new List<Ingredient>
                    {
                        new Ingredient { Name = "Water" }
                    }
                },
                new Recipe
                {
                    Name = "Cool drink",
                    Text = "Drink. Optionally with some more ice.",
                    Author = users[rnd.Next(users.Count)],
                    Category = drinks,
                    Ingredients = new List<Ingredient>
                    {
                        new Ingredient { Name = "Water" },
                        new Ingredient { Name = "Ice" }
                    }
                },
                new Recipe
                {
                    Name = "Cinnamon rolls",
                    Text = "Dough:\n" +
                           "1 cup warm milk (110 degrees F/45 degrees C)\n" +
                           "3 (0.6 ounce) cakes cake yeast\n" +
                           "6 1/2 tablespoons butter, room temperature\n" +
                           "2 eggs\n" +
                           "1/2 teaspoon ground cardamom\n" +
                           "2 tablespoons white sugar\n" + 
                           "1/4 teaspoon salt\n" +
                           "4 cups all-purpose flour, or as needed\n\n" +
                           "Filling:\n" +
                           "2/3 cup butter, softened\n" +
                           "1/2 cup white sugar\n" +
                           "2 tablespoons ground cinnamon\n\n" +

                           "Pour the warm milk into a mixing bowl and mash in the fresh cake yeast. Mix in 6 1/2 tablespoons of soft butter, eggs, cardamom, 2 tablespoons sugar, salt, and 3 1/2 cups of the flour. Use a wooden spoon to mix the dough. If it's very sticky, mix in the remaining 1/2 cup of flour. Cover the bowl and let the dough rise for 30 minutes.\n" + 
                           "Cream together the 2/3 cup butter and 1/2 cup sugar. Stir in the cinnamon.\n" + 
                           "Transfer the dough to a floured surface and knead it until it's firm, about 3 minutes. Divide the dough in half; roll each half into a rectangle no more than 1/2 inch thick. Spread each rectangle with half the filling.\n" + 
                           "Stack one layer of dough and filling on top of the other rectangle of dough, filling-side up. Roll the dough up, starting with the edge closest to you, to form a long log. Cut the log into 1 inch-thick slices.\n" +
                           "Preheat an oven to 425 degrees F (220 degrees C). Line a baking sheet with parchment paper, or grease a baking dish or two cake pans.\n" +
                           "Place the rolls on the prepared baking sheet, spacing them about 3 inches apart. If you like pull-apart rolls, arrange them in a greased baking dish or cake pans, spacing the rolls about 1 inch apart. Place the uneven end pieces on the baking sheet cut-side up for the best presentation. Let the rolls rest 20 minutes before baking.\n" +
                           "Bake the snails in the preheated oven until golden brown, about 10 minutes. Pull-apart rolls will take longer to bake: after 10 minutes, reduce the oven temperature to 350 degrees F (175 degrees C) and bake the rolls until the sides are fully set, about 10 minutes longer. Cover the baking dish with foil if the rolls begin to get too brown.\n",
                    Author = users[rnd.Next(users.Count)],
                    Category = desserts,
                    Ingredients = new List<Ingredient>
                    {
                        new Ingredient { Name = "Milk" },
                        new Ingredient { Name = "Yeast" },
                        new Ingredient { Name = "Cinnamon" },
                        new Ingredient { Name = "Sugar" },
                        new Ingredient { Name = "Cardamon" },
                        new Ingredient { Name = "Flour" },
                        new Ingredient { Name = "Butter" }
                    }
                },
                new Recipe
                {
                    Name = "Chana masala",
                    Text = "Makes 2 large portions.\n3 tbsp of oil at low heat.\n\nFry a pinch of asafetida, 1 tsp cummin seeds, 2 tsp coriander, ½ tsp chili powder, ½ tsp turmeric, ½ tsp garam masala, 1 tsp salt and 3-4 cm chopped ginger until it smells fragrant, be careful not to burn the spices. " +
                           "\nMix in 1 tbsp of flour to make a thickener and let heat for about 1 minute. Pour in 1 can of crushed tomatoes and 1 can of chickpeas. Stir, raise the temperature and let it simmer for about 5-6 minutes.\nChop a handfull of cilantro and stir in.\n\nEat with rice.",
                    Author = users[rnd.Next(users.Count)],
                    Category = food,
                    Ingredients = new List<Ingredient>
                    {
                          new Ingredient() { Name = "Canola oil"},
                          new Ingredient() { Name = "Chickpea"},
                          new Ingredient() { Name = "Asafetida"},
                          new Ingredient() { Name = "Cumin seed"},
                          new Ingredient() { Name = "Coriander"},
                          new Ingredient() { Name = "Turmeric"},
                          new Ingredient() { Name = "Chili powder"},
                          new Ingredient() { Name = "Salt"},
                          new Ingredient() { Name = "Garam masala"},
                          new Ingredient() { Name = "Ginger"},
                          new Ingredient() { Name = "Flour"},
                          new Ingredient() { Name = "Crushed tomato"},
                          new Ingredient() { Name = "Cilantro"},
                    }
                },
                new Recipe
                {
                    Name = "Parsnip soup with coconut cream",
                    Text = "Makes 2 large portions for a main course or 4 portions for a starter course.\n\n" +
                           "Peel 500 g parsnip and cut it into pieces. Let it boil in 4 dl of water with 2 cubes vegetable broth and 1.5 dl coconut cream until soft.\n\n" +
                           "Pour in another 1.5 dl of coconut cream and mix the soup with a blender until smooth. " +
                           "Pour in more water if the soup is too thick, adjust taste with salt.\n" +
                           "Fry 1/3 tsp cardamom seeds and 10 black pepper grains at low heat in a dry pan until it smells fragrant. Use a mortar to crush the spices.\n" +
                           "Heat oil and fry 1 tbsp of grated lemon peel very hastily. " +
                           "\n\nServe soup with a topping of crushed spices, fried lemon peel and a little bit of chopped cilantro.",
                    Author = users[rnd.Next(users.Count)],
                    Category = food,
                    Ingredients = new List<Ingredient>
                    {
                          new Ingredient() { Name = "Parsnip"},
                          new Ingredient() { Name = "Coconut cream"},
                          new Ingredient() { Name = "Water"},
                          new Ingredient() { Name = "Broth"},
                          new Ingredient() { Name = "Salt"},
                          new Ingredient() { Name = "Black pepper grain"},
                          new Ingredient() { Name = "Cardamom seed"},
                          new Ingredient() { Name = "Lemon peel"},
                          new Ingredient() { Name = "Canola oil"},
                          new Ingredient() { Name = "Cilantro"}
                    }

                },
                new Recipe
                {
                    Name = "Lentil soup",
                    Text = "Makes 4 portions.\n\nPeel and chop 1 and ½ onion.\nPeel 5 potatoes and cut them into bite sized chunks.\n" +
                           "Heat oil on low and fry the onion, 1 finely chopped clove of garlic, 1 tsp salt, ½ tsp cumin, 1 tsp coriander and 2 tsp curry until onion is soft.\n" +
                           "Pour in 1.3 liters of water. Put in potatoes, 250-300 g red lentils and 2 cubes vegetable broth. " +
                           "\nLet it boil for 10 minutes. Remove from heat and stir in 1 tbsp lemon juice and 1 dl soy cream.",
                    Author = users[rnd.Next(users.Count)],
                    Category = food,
                    Ingredients = new List<Ingredient>
                    {
                        new Ingredient { Name = "Canola oil" },
                        new Ingredient { Name = "Onion" },
                        new Ingredient { Name = "Potato" },
                        new Ingredient { Name = "Red lentil" },
                        new Ingredient { Name = "Garlic" },
                        new Ingredient { Name = "Water" },
                        new Ingredient { Name = "Broth" },
                        new Ingredient { Name = "Salt" },
                        new Ingredient { Name = "Black pepper" },
                        new Ingredient { Name = "Cumin" },
                        new Ingredient { Name = "Coriander" },
                        new Ingredient { Name = "Curry" },
                        new Ingredient { Name = "Lemon juice" },
                        new Ingredient { Name = "Soy cream" }
                    }
                },
                new Recipe
                {
                    Name = "Gyoza dip sauce",
                    Text = "Mix 2 tbsp soy, 2 tbsp rice vinegar, 1 tbsp sriracha and 1 tbsp sesam oil.\n\nDip and enjoy!",
                    Author = users[rnd.Next(users.Count)],
                    Category = food,
                    Ingredients = new List<Ingredient>
                    {
                        new Ingredient { Name = "Soy" },
                        new Ingredient { Name = "Rice vinegar" },
                        new Ingredient { Name = "Sriracha" },
                        new Ingredient { Name = "Sesame oil" }
                    }
                },
                new Recipe
                {
                    Name = "Guacamole",
                    Text = "Peel 2 avocados and mush them up in a bowl.\nChop 1 tomato, ½ red onion and 1 red chili. " +
                           "Stir everything together with 1 tbsp lemon juice.\nAdjust taste with salt, black pepper and lemon juice. " +
                           "\n\nDip and enjoy!",
                    Author = users[rnd.Next(users.Count)],
                    Category = food,
                    Ingredients = new List<Ingredient>
                    {
                        new Ingredient { Name = "Avocado" },
                        new Ingredient { Name = "Tomato" },
                        new Ingredient { Name = "Red onion" },
                        new Ingredient { Name = "Chili" },
                        new Ingredient { Name = "Lemon juice" },
                        new Ingredient { Name = "Black pepper" },
                        new Ingredient { Name = "Salt" }
                    }
                },
                new Recipe
                {
                    Name = "Skånsk mustard",
                    Text = "Makes about 3 dl of mustard.\n\nBoil ½ dl yellow mustard seeds and ½ dl brown mustard seeds in 1 and ½ dl of water for about 2 minutes.\n\n" +
                           "Mix with a blender until smooth. Pour in 1 tbsp apple cider vinegar, 2 tbsp of sugar, 1 tsp salt and ½ dl canola oil and stir. " +
                           "\n\nLet it sit in the fridge for a couple of days before eating for a better taste.",
                    Author = users[rnd.Next(users.Count)],
                    Category = food,
                    Ingredients = new List<Ingredient>
                    {
                        new Ingredient { Name = "Yellow mustard seed" },
                        new Ingredient { Name = "Brown mustard seed" },
                        new Ingredient { Name = "Water" },
                        new Ingredient { Name = "Apple cider vinegar" },
                        new Ingredient { Name = "Sugar" },
                        new Ingredient { Name = "Canola oil" },
                        new Ingredient { Name = "Salt" }
                    }
                },
                new Recipe
                {
                    Name = "Marinated bean salad",
                    Text = "Makes salad for 4-6 people.\n\nDice 2 tomatoes and chop 1 leek. Mix with 200 g black beans and 200 g white beans, boiled or from can.\n" +
                           "Make a dressing by mixing 3 tbsp olive oil, 3 tbsp balsamic vinegar, 2 finely chopped cloves of garlic, ½ tsp salt and a pinch of pepper.\n\n" +
                           "Pour the dressing over the salad and let it sit in the fridge for about half an hour.",
                    Author = users[rnd.Next(users.Count)],
                    Category = food,
                    Ingredients = new List<Ingredient>
                    {
                        new Ingredient { Name = "White bean" },
                        new Ingredient { Name = "Black bean" },
                        new Ingredient { Name = "Tomato" },
                        new Ingredient { Name = "Leek" },
                        new Ingredient { Name = "Olive oil" },
                        new Ingredient { Name = "Balsamic vinegar" },
                        new Ingredient { Name = "Salt" },
                        new Ingredient { Name = "Black pepper" },
                        new Ingredient { Name = "Garlic" }
                    }
                },
                new Recipe
                {
                    Name = "Green bean salad",
                    Text = "Serves 3-4 people.\n\nSteam 250 g haricots verts, chop 1 red onion and mix together. " +
                           "Make a dressing by mixing 1 ½ tbsp olive oil, 1 ½ tbsp balsamic vinegar, 1 tsp salt, ½ tsp sugar and ½ tsp black pepper.\n\n" +
                           "Mix the dressing with the salad.",
                    Author = users[rnd.Next(users.Count)],
                    Category = food,
                    Ingredients = new List<Ingredient>
                    {
                        new Ingredient { Name = "Haricots verts" },
                        new Ingredient { Name = "Red onion" },
                        new Ingredient { Name = "Olive oil" },
                        new Ingredient { Name = "Balsamic vinegar" },
                        new Ingredient { Name = "Salt" },
                        new Ingredient { Name = "Sugar" },
                        new Ingredient { Name = "Black pepper" }
                    }
                },
                new Recipe
                {
                    Name = "Potatocurry with sesame seeds",
                    Text = "Makes side dish for about 4 people.\n\nBoil 4 big potatoes with the peel still on until soft. " +
                           "Let it cool, peel and cut it into bite sized chunks. Heat 1 tbsp canola oil on medium heat and fry 1 tsp cumin seeds, 1 tsp coriander seeds, 2 tbsp yellow mustard seeds while constantly stirring for 1 minute.\n" +
                           "Add 2 tbsp sesame seeds and fry for 1-2 more minutes.\n" +
                           "Add ½ tsp turmeric, 1 tsp chopped chili, 2 tsp grated lemon peel and 2 tbsp lemon juice. Stir.\n" +
                           "Add the potato pieces and mix until it is fully coated and the potatoes are heated through.\n\n" + 
                           "Adjust taste with salt and pepper.",
                    Author = users[rnd.Next(users.Count)],
                    Category = food,
                    Ingredients = new List<Ingredient>
                    {
                        new Ingredient { Name = "Potato" },
                        new Ingredient { Name = "Canola oil" },
                        new Ingredient { Name = "Cumin seed" },
                        new Ingredient { Name = "Yellow mustard seed" },
                        new Ingredient { Name = "Sesame seed" },
                        new Ingredient { Name = "Turmeric" },
                        new Ingredient { Name = "Lemon peel" },
                        new Ingredient { Name = "Chili" },
                        new Ingredient { Name = "Lemon juice" },
                        new Ingredient { Name = "Salt" },
                        new Ingredient { Name = "Black pepper" }
                    }
                },
                new Recipe
                {
                    Name = "Sweet pesto-tapenade",
                    Text = "Mix 3 ½ dl tightly packed basil, 1 dl walnuts and 2 cloves of garlic in a blender.\n" +
                           "Pour in ½ dl olive oil, 1/3 dl walnut oil, ½ tsp grated lemon peel and ½ dl maple syrup and mix until thick and creamy.\n" +
                           "Add 2/3 tsp salt and ½ tsp black pepper.\n\nStir. Eat with pasta dish.",
                    Author = users[rnd.Next(users.Count)],
                    Category = food,
                    Ingredients = new List<Ingredient>
                    {
                        new Ingredient { Name = "Fresh basil" },
                        new Ingredient { Name = "Walnut" },
                        new Ingredient { Name = "Garlic" },
                        new Ingredient { Name = "Olive oil" },
                        new Ingredient { Name = "Walnut oil" },
                        new Ingredient { Name = "Maple syrup" },
                        new Ingredient { Name = "Lemon peel" },
                        new Ingredient { Name = "Salt" },
                        new Ingredient { Name = "Black pepper" }
                    }
                },
                new Recipe
                {
                    Name = "Kebab sauce",
                    Text = "Mix 5 dl soygurt with 2 dl soy cream.\nAdd 1 finely chopped chili, 1 tsp salt, ½ tsp black pepper, ½ tsp coriander, ½ tsp chili sauce and ½ tsp sriracha. Stir well.\n" +
                           "Adjust taste with all ingredients, be careful with the chili sauce though - it can easily make the sauce too sweet.",
                    Author = users[rnd.Next(users.Count)],
                    Category = food,
                    Ingredients = new List<Ingredient>
                    {
                        new Ingredient { Name = "Soygurt" },
                        new Ingredient { Name = "Soy cream" },
                        new Ingredient { Name = "Salt" },
                        new Ingredient { Name = "Black pepper" },
                        new Ingredient { Name = "Coriander" },
                        new Ingredient { Name = "Sriracha" },
                        new Ingredient { Name = "Chili" },
                        new Ingredient { Name = "Chili sauce" }
                    }
                },
                new Recipe
                {
                    Name = "Ovenbaked mint chocolate pears",
                    Text = "Makes 6-8 yummy pears.\n\nHeat oven to 175 degrees C.\nCoat a plate with butter and put your pears on it. Add an after eight on top of each pear and let it sit in the oven for 5-6 minutes.\n\n" +
                           "Serve with ice cream.",
                    Author = users[rnd.Next(users.Count)],
                    Category = desserts,
                    Ingredients = new List<Ingredient>
                    {
                        new Ingredient { Name = "Pickled pear" },
                        new Ingredient { Name = "After eight" },
                        new Ingredient { Name = "Ice cream" },
                        new Ingredient { Name = "Butter"}
                    }
                },
                new Recipe
                {
                    Name = "Asparagus with lemon and ginger.",
                    Text = "Serves 4 people.\n\nTake 1 bunch of fresh asparagus, break off the lower part of their stems.\nCut them into about 2 or 3 pieces per asparagus. " +
                           "Chop 2 cm ginger finely.\nHeat olive oil on medium heat.\nAdd 1 tsp cumin seeds, 2 tsp lemon juice, ½ tsp salt, ½ tsp black pepper and the ginger. " +
                           "Let it fry while stirring for 1 minute.\nAdd the asparagus, coat it and fry for another 4-5 minutes.",
                    Author = users[rnd.Next(users.Count)],
                    Category = food,
                    Ingredients = new List<Ingredient>
                    {
                        new Ingredient { Name = "Asparagus" },
                        new Ingredient { Name = "Olive oil" },
                        new Ingredient { Name = "Cumin seed" },
                        new Ingredient { Name = "Ginger"},
                        new Ingredient { Name = "Lemon juice"},
                        new Ingredient { Name = "Salt"},
                        new Ingredient { Name = "Black pepper"}
                    }
                }
            };

            IRecipeRepository repo = new RecipeRepository(context);
            recipes.ForEach(r => repo.Save(r));
        }

        private static User User(string username, string password, string email)
        {
            return new User
            {
                Username = username,
                UnhashedPassword = password,
                ConfirmPassword = password,
                Email = email,
                ConfirmEmail = email
            };
        }
    }

    internal class DropCreateAndSeedIfChange : DropCreateDatabaseIfModelChanges<KelperDbContext>
    {
        protected override void Seed(KelperDbContext context)
        {
            Seeder.Seed(context);
        }
    }

    internal class DropCreateAndSeedAlways : DropCreateDatabaseAlways<KelperDbContext>
    {
        protected override void Seed(KelperDbContext context)
        {
            Seeder.Seed(context);
        }
    }
}
